package com.ay.java.proxy.jdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author: ay
 * @Date: 2021/6/11
 * @Description: jdk动态代理类
 **/
public class JdkProxy implements InvocationHandler {

    private Object target;

    /**
     * 创建代理类
     * @return
     */
    public Object createProxy(Object target){
        this.target = target;
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("【jdk动态代理】检查权限");
        proxy = method.invoke(target,args);
        System.out.println("【jdk动态代理】记录日志");
        return proxy;
    }
}
